from .xt_future_utils import http_get_request, api_key_get, api_key_post


class XTFuture:

    def __init__(self, url, access_key, secret_key):
        self.__url = url
        self.__access_key = access_key
        self.__secret_key = secret_key

    def get_signal_market_config(self, symbol):
        """
        symbol:
        :return:获取单个交易对的配置信息
        """
        params = {"symbol": symbol}
        url = self.__url + "/future/market" + '/v1/public/symbol/detail'
        return http_get_request(url, params=params)

    def get_all_market_config(self):
        """
        symbol:
        :return:获取单个交易对的配置信息
        """
        params = {}
        url = self.__url + "/future/market" + '/v1/public/symbol/list'
        return http_get_request(url, params=params)

    def get_all_pair_info(self):
        """
        :return:获取交易对币种
        """
        url = self.__url + "/future/market" + '/v1/public/symbol/coins'
        return http_get_request(url, params="")

    def get_funding_rate(self, symbol):
        """
        :return:获取资金费率
        """
        params = {"symbol": symbol}
        url = self.__url + "/future/market" + '/v1/public/q/funding-rate'
        return http_get_request(url, params=params)

    def get_agg_tiker(self, symbol):
        """
        :return:返回聚合行情
        """
        params = {"symbol": symbol}
        url = self.__url + "/future/market" + '/v1/public/q/agg-ticker'
        return http_get_request(url, params=params)

    def get_agg_tikers(self):
        """
        :return:返回聚合行情
        """
        params = {}
        url = self.__url + "/future/market" + '/v1/public/q/agg-tickers'
        return http_get_request(url, params=params)

    def get_last_price(self, symbol, length):
        """
        :return:获取最近成交记录
        """
        params = {"symbol": symbol, "num": length}
        url = self.__url + "/future/market" + '/v1/public/q/deal'
        return http_get_request(url, params=params)

    def get_depth(self, symbol, depth):
        """
        :return:获取深度数据
        """
        params = {"symbol": symbol, "level": depth}
        url = self.__url + "/future/market" + '/v1/public/q/depth'
        return http_get_request(url, params=params)
    
    def get_trades(self, symbol, num):
        """
        :return:获取成交数据
        """
        params = {"symbol": symbol, "num": num}
        url = self.__url + "/future/market" + '/v1/public/q/deal'
        return http_get_request(url, params=params)

    def get_account_capital(self):
        """
        :return:获取账户资金
        """
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/user" + '/v1/balance/list'
        url = self.__url + path
        return api_key_get(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                           bodymod=bodymod, params={})

    def get_history_order(self):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/list-history'
        url = self.__url + path
        return api_key_get(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                           bodymod=bodymod, params={})

    def get_account_order(self, symbol, state, page, size):
        """
        state:
        订单状态 NEW：新建订单（未成交）；
        PARTIALLY_FILLED：部分成交；
        FILLED：全部成交；CANCELED：用户撤销；
        REJECTED：下单失败；EXPIRED：已过期；
        UNFINISHED：未完成；HISTORY：（历史）
        """
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order-entrust/list'
        url = self.__url + path
        params = {
            "symbol": symbol,
            "state": state,
            "page": page,
            "size": size
        }
        return api_key_get(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                           bodymod=bodymod, params=params)

    def get_position(self, symbol):
        """
        获取持仓信息
        :return:
        """
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/user" + '/v1/position/list'
        url = self.__url + path
        params = {
            "symbol": symbol,
        }
        return api_key_get(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                           bodymod=bodymod, params=params)

    def get_index_price(self, symbol, size):
        """
        :return:获取深度数据
        """
        params = {"symbol": symbol, "size": size}
        url = self.__url + "/future/market" + '/v1/public/q/symbol-mark-price'
        return http_get_request(url, params=params)

    def get_all_index_price(self):
        params = {}
        url = self.__url + "/future/market" + '/v1/public/q/mark-price'
        return http_get_request(url, params=params)

    def get_kline(self, symbol, interval, limit):
        """
        :return:获取深度数据
        """
        params = {"symbol": symbol, "interval": interval, 'limit': limit}
        url = self.__url + "/future/market" + '/v1/public/q/kline'
        return http_get_request(url, params=params)

    def get_order_id(self, order_id):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/detail'
        url = self.__url + path
        params = {
            "orderId": order_id
        }
        return api_key_get(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                           bodymod=bodymod, params=params)

    def get_batch_order_id(self, order_id_list: list):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/list-by-ids'
        url = self.__url + path
        order_id_query = ",".join(order_id_list)
        params = {
            "ids": order_id_query
        }
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def send_order(self, symbol, price, amount, order_side, order_type, position_side, timeInForce):
        """
        :return:
        """
        params = {"orderSide": order_side,
                  "orderType": order_type,
                  "origQty": amount,
                  "positionSide": position_side,
                  "symbol": symbol,
                  "price": price,
                  'timeInForce': timeInForce}

        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/create'
        url = self.__url + path
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def send_batch_order(self, order_list: list):
        """
        :return:获取账户资金
        """
        params = {"list": str(order_list)}

        bodymod = "x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/create-batch'
        url = self.__url + path
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def cancel_order(self, order_id):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order/cancel'
        url = self.__url + path
        params = {
            "orderId": order_id
        }
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def cancel_all_order(self):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/trade" + '/v1/order-entrust/cancel-all'
        url = self.__url + path
        params = {}
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def set_account_leverage(self, leverage, position_side, symbol):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/user" + '/v1/position/adjust-leverage'
        url = self.__url + path
        params = {
            "leverage": leverage,
            "positionSide": position_side,
            "symbol": symbol
        }
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

    def set_merge_position(self, symbol):
        bodymod = "application/x-www-form-urlencoded"
        path = "/future/user" + '/v1/position/merge'
        url = self.__url + path
        params = {
            "symbol": symbol
        }
        return api_key_post(path=path, url=url, access_key=self.__access_key, secret_key=self.__secret_key,
                            bodymod=bodymod, params=params)

